package com.atxiaodei.kabudashejimoshi.单例模式.容器式单例模式;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName ContainerSingleton
 * @Deacription TODO
 * @Author 卡布达
 * @Date 2021/10/12 13:07
 * @Version 1.0
 **/
public class ContainerSingleton {


    private ContainerSingleton(){}

    private static Map<String,Object> ioc=new ConcurrentHashMap<>();

    public static Object getBean(String className){

        synchronized (ioc){

            if (!ioc.containsKey(className)){

                Object obj=null;

                try {

                    obj= Class.forName(className).newInstance();

                } catch (InstantiationException e) {

                    e.printStackTrace();

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                return obj;
            }else {

                return ioc.get(className);

            }
        }
    }
}
